/*
Janet Putnam
CS 362
Assignment 3
Unit Test 2 - Unit Test of isGameOver

Include the following lines in make file:

unittest2:  unittest2.c dominion.o rngs.o
	gcc -o unittest2 -g unittest2.c dominion.o rngs.o $(CFLAGS)

*/

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>



/* Copy of function from dominion.c for reference purposes, can be removed from code
int isGameOver(struct gameState *state) {
  int i;
  int j;
	
  //if stack of Province cards is empty, the game ends
  if (state->supplyCount[province] == 0)
    {
      return 1;
    }

  //if three supply pile are at 0, the game ends
  j = 0;
  for (i = 0; i < 25; i++)
    {
      if (state->supplyCount[i] == 0)
	{
	  j++;
	}
    }
  if ( j >= 3)
    {
      return 1;
    }

  return 0;
}
*/
int isGameOver(struct gameState *state);

int isGameOverTest(int print_pass)
{
	int result;
	int check;
	int r;
	int l;
	//setup initial struct gameState
	struct gameState G;
	
	memset(&G, 0, sizeof(struct gameState));
	
	//select cards in game
	int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, sea_hag, tribute, smithy};

	//initialize gameState struct
	initializeGame(2, k, 4, &G);
	
	if(print_pass == 0)
	{
		//check values of cards after creation
		printf("Initial Number of province cards %d \n", (G.supplyCount[province]));
		printf("Initial Number of estate cards %d \n", (G.supplyCount[estate]));
		printf("Initial Number of duchy cards %d \n", (G.supplyCount[duchy]));
		printf("Initial Number of gold cards %d \n", (G.supplyCount[gold]));
		printf("Initial Number of silver cards %d \n", (G.supplyCount[silver]));
		printf("Initial Number of copper cards %d \n", (G.supplyCount[copper]));
		printf("Initial Number of adventurer cards %d \n", (G.supplyCount[adventurer]));
		printf("Initial Number of gardens cards %d \n", (G.supplyCount[gardens]));
		printf("Initial Number of embargo cards %d \n", (G.supplyCount[embargo]));
		printf("Initial Number of village cards %d \n", (G.supplyCount[village]));
		printf("Initial Number of minion cards %d \n", (G.supplyCount[minion]));
		printf("Initial Number of mine cards %d \n", (G.supplyCount[mine]));
		printf("Initial Number of cutpurse cards %d \n", (G.supplyCount[cutpurse]));
		printf("Initial Number of sea_hag cards %d \n", (G.supplyCount[sea_hag]));
		printf("Initial Number of tribute cards %d \n", (G.supplyCount[tribute]));
		printf("Initial Number of smithy cards %d \n", (G.supplyCount[smithy]));
		printf("Initial Number of curse cards %d \n", (G.supplyCount[curse]));
	}
	

	//randomly set number of card supply to 0
	for (l = 0; l<= 10; l++)
	{
		r = (rand() % 16);
		if (r == 0)
		{
			(G.supplyCount[province]) = 0;
		}
		else if (r == 1)
		{
			(G.supplyCount[estate]) = 0;
		}
		else if (r == 2)
		{
			(G.supplyCount[duchy]) = 0;
		}
		else if (r == 3)
		{
			(G.supplyCount[silver]) = 0;
		}
		else if (r == 4)
		{
			(G.supplyCount[copper]) = 0;
		}
		else if (r == 5)
		{
			(G.supplyCount[adventurer]) = 0;
		}
		else if (r == 6)
		{
			(G.supplyCount[gardens]) = 0;
		}
		else if (r == 7)
		{
			(G.supplyCount[embargo]) = 0;
		}
		else if (r == 8)
		{
			(G.supplyCount[village]) = 0;
		}
		else if (r == 9)
		{
			(G.supplyCount[minion]) = 0;
		}
		else if (r == 10)
		{
			(G.supplyCount[mine]) = 0;
		}
		else if (r == 11)
		{
			(G.supplyCount[cutpurse]) = 0;
		}
		else if (r == 12)
		{
			(G.supplyCount[sea_hag]) = 0;
		}
		else if (r == 13)
		{
			(G.supplyCount[tribute]) = 0;
		}
		else if (r == 14)
		{
			(G.supplyCount[smithy]) = 0;
		}
		else if (r == 15)
		{
			(G.supplyCount[curse]) = 0;
		}
		else
		{
			printf("Error with random number generator function.\n");
		}
		
		if (r==0)
		{
			//pull number of provice cards from struct
			check = G.supplyCount[province];
		
			// run the function in question
			result = isGameOver(&G);
			if((result == 0) && (check == 0))
			{
				printf("FAIL:  Number of province cards is equal to zero and game did not end.\n");
			}
			else if (result == 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  Number of province cards is equal to zero and game ended.\n");
				}
			}
		}
		else
		{
			// run the function in question
			result = isGameOver(&G);
			if((result == 0) && (l >= 3))
			{
				printf("FAIL:  %d number of cards types have zero cards and game did not end.\n", l+1);
			}
			else if (result == 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  %d number of card types have zero cards and game ended.\n", l+1);
				}
			}
			else if ((result == 0) && (l < 3))
			{
				if(print_pass == 0)
				{
					printf("PASS:  %d number of card types have zero cards and game has not ended.\n", l+1);
				}
				
			}
			else
			{
				printf("Test in unknown state.");
			}
		
		
		}
		
	}
	
	return 0;



}


int main(int argc, char **argv)
{	
	int print_pass;
	//if print_pass = 0, then results will print for passing tests.
	//if print_pass = 1, then results will not print for passing tests.
	//regardless of print_pass state, failing tests will print results.
	
	print_pass = atoi(argv[1]);
	if ((print_pass != 1 ) || (print_pass != 0))
	{
		print_pass = 0;
	}
	srand(time(NULL));
	isGameOverTest(print_pass);
	return 0;
}